﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using RED_NET_Core.DAL;
using RED_NET_Core.Entity;
using RED_NET_Core.BLL;

namespace RED_NET.ContentPage
{
    public partial class OverPhoto : System.Web.UI.Page
    {
        public static DataTable dt = null;
        public int VISIT_ID,Album_ID,USER_ID;
        public static int CurrentPhotoID;
        public int RowInDt;
        Album_Photo album_Photo;
        PhotoClass photo;
        //public String[] arrImages, arrImages_s;
        public int ImageCount;
        public String arrImages, arrImages_s;
        protected void Page_Load(object sender, EventArgs e)
        {
            VISIT_ID = Int32.Parse(Request.Params["VISIT_ID"].ToString());
            Album_ID = Int32.Parse(Request.Params["Album_ID"].ToString());
            CurrentPhotoID = Int32.Parse(Request.Params["Photo_ID"].ToString());
            if (!IsPostBack)
            {
                //检测用户是否登录
                if (Session["USER"] == null)
                {
                    Button1.Visible = Button2.Visible = false;//匿名观看
                }
                else
                {
                    Users user = (Users)Session["USER"];
                    USER_ID = user.UserId;
                    if (VISIT_ID != USER_ID) Button4.Visible = Button3.Visible = false;//非本人观看
                }
                album_Photo = new Album_Photo();
                try
                {
                    OverPhoto.dt = album_Photo.GetAllPhotoByAlbum(Album_ID);          //获取该相册所有相片存入dt
                    photo = album_Photo.GetPhotoInf(CurrentPhotoID);
                }
                catch (Exception ex)
                {
                    //将错误信息插入数据库
                    if (Session["USER"] != null)
                    {
                        Users user = (Users)Session["USER"];
                        USER_ID = user.UserId;
                        DateTime time = DateTime.Now;
                        ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                    }
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                
                //插入操作datatable的语句，此处不得已违反三层架构，以后能改时再改。
                int i;
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (Int32.Parse(dt.Rows[i]["_PhotoId"].ToString()) == CurrentPhotoID)
                    {
                        RowInDt = i;
                        break;
                    }
                }
                ViewState["RowInDt"] = RowInDt;


                //判断是否有上一页或者下一页
                if (i + 1 >= dt.Rows.Count)
                {
                    Button2.Enabled = false;
                }
                else Button2.Enabled = true;

                if (i - 1 < 0)
                {
                    Button1.Enabled = false;
                }
                else Button2.Enabled = true;

                string imageUrl = photo.PhotoUrl;
                Image1.ImageUrl = imageUrl;
                Button4.Attributes.Add("onClick", "return confirm('确定删除该相片？')");

                //提供给JS的URL
                ArrayList al,alist;
                try
                {
                    al = album_Photo.GetAllPhotoByAlbum_JS(Album_ID);
                    ImageCount = al.Count;//前台用，图片数
                    arrImages = "";
                    for (i = 0; i < ImageCount - 1; i++)
                        arrImages = arrImages + al[i].ToString() + ",";
                    arrImages = arrImages + al[ImageCount - 1];
                    //缩略图URL
                    alist = album_Photo.GetAllSmallPhotoByAlbum_JS(Album_ID);
                    for (i = 0; i < ImageCount - 1; i++)
                        arrImages_s = arrImages_s + al[i].ToString() + ",";
                    arrImages_s = arrImages_s + al[ImageCount - 1];
                }
                catch (Exception ex)
                {
                    //将错误信息插入数据库
                    if (Session["USER"] != null)
                    {
                        Users user = (Users)Session["USER"];
                        USER_ID = user.UserId;
                        DateTime time = DateTime.Now;
                        ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                    }
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Write("<script>location.href='UpdatePhoto.aspx?VISIT_ID=" + VISIT_ID + 
                "&Album_Id=" + Album_ID + "&Photo_ID=" + CurrentPhotoID + "';</script>");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Album_Photo album_photo = new Album_Photo();
            try
            {
                album_photo.DeletePhoto(OverPhoto.CurrentPhotoID);
            }
            catch (Exception ex)
            {
                //将错误信息插入数据库
                if (Session["USER"] != null)
                {
                    Users user = (Users)Session["USER"];
                    USER_ID = user.UserId;
                    DateTime time = DateTime.Now;
                    ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                }
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            Response.Write("<script>alert('删除成功！');location.href='Photo.aspx?VISIT_ID=" + VISIT_ID + "&Album_Id=" + Album_ID + "';</script>");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            RowInDt = Int32.Parse(ViewState["RowInDt"].ToString());
            RowInDt++;
            ViewState["RowInDt"] = RowInDt;
            OverPhoto.CurrentPhotoID = Int32.Parse(OverPhoto.dt.Rows[RowInDt]["_PhotoId"].ToString());
            album_Photo = new Album_Photo();
            try
            {
                photo = album_Photo.GetPhotoInf(CurrentPhotoID);
            }
            catch (Exception ex)
            {
                //将错误信息插入数据库
                if (Session["USER"] != null)
                {
                    Users user = (Users)Session["USER"];
                    USER_ID = user.UserId;
                    DateTime time = DateTime.Now;
                    ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                }
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            if (photo != null)
            {
                string imageUrl = photo.PhotoUrl;
                Image1.ImageUrl = imageUrl;
            }

            if (RowInDt + 1 >= dt.Rows.Count)
            {
                Button2.Enabled = false;
            }
            else Button2.Enabled = true;

            if (RowInDt - 1 < 0)
            {
                Button1.Enabled = false;
            }
            else Button1.Enabled = true;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            RowInDt = Int32.Parse(ViewState["RowInDt"].ToString());
            RowInDt--;
            ViewState["RowInDt"] = RowInDt;
            OverPhoto.CurrentPhotoID = Int32.Parse(OverPhoto.dt.Rows[RowInDt]["_PhotoId"].ToString());
            album_Photo = new Album_Photo();
            try
            {
                photo = album_Photo.GetPhotoInf(CurrentPhotoID);
            }
            catch (Exception ex)
            {
                //将错误信息插入数据库
                if (Session["USER"] != null)
                {
                    Users user = (Users)Session["USER"];
                    USER_ID = user.UserId;
                    DateTime time = DateTime.Now;
                    ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                }
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            if (photo != null)
            {
                string imageUrl = photo.PhotoUrl;
                Image1.ImageUrl = imageUrl;
            }

            if (RowInDt + 1 >= dt.Rows.Count)
            {
                Button2.Enabled = false;
            }
            else Button2.Enabled = true;

            if (RowInDt - 1 < 0)
            {
                Button1.Enabled = false;
            }
            else Button1.Enabled = true;
        }
    }
}